/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-04-06
 * Time: 18:53
 */
public class MySingleList {

    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }
    public void show() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void show(ListNode newHead) {
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(key == cur.val) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    public void addFirst(int date) {
        ListNode node = new ListNode(date);
        node.next = head;
        head = node;
    }
    public void addLast(int date) {
        ListNode node = new ListNode(date);
        if(head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }
    public void add(int date, int index) {
        ListNode node = new ListNode(date);
        if(index < 0 || index > size()) {
            return;
        }
        if(index == 0) {
            addFirst(date);
            return;
        } else if (index == size()) {
            addLast(date);
            return;
        }
        ListNode cur = findIndex(index);
        node.next = cur.next;
        cur.next = node;
    }
    private ListNode findIndex(int index) {
        ListNode cur = head;
        while (0 != index - 1) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    public void delete(int key) {
        if(!contains(key) || head == null) {
            System.out.println("没有这个数据");
            return;
        }
        if (head.val == key) {
            head = head.next;
            return;
        }
        ListNode prev = searchPrev(key);
        ListNode del = prev.next;
        prev.next = del.next;
    }
    private ListNode searchPrev(int key) {
        ListNode prev = head;
        while (prev.next.val != key) {
            prev = prev.next;
        }
        return prev;
    }
    public void removeAllKey(int key) {
        if(head ==null) {
            return;
        }
        ListNode pre = head;
        ListNode cur = pre.next;
        while(cur != null) {
            if(cur.val == key) {
                pre.next = cur.next;
                cur = cur.next;
            } else {
                pre = cur;
                cur = cur.next;
            }
        }
        if (head.val == key) {
            head = head.next;
        }
    }
    public void clear() {
        //this.head = null;
        while (head != null) {
            ListNode headNext = head.next;
            head.next = null;
            head = headNext;
        }
    }



    public ListNode reverse() {
        if(head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    public ListNode findMid() {
        if(head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public ListNode FindKthToTail(int k) {
        if(k <= 0 || head == null) {
            return  null;
        }
        ListNode fast = head;
        ListNode slow = head;
        int count = k - 1;
        while(count != 0) {
            fast = fast.next;
            count--;
            if(fast == null) {
                return null;
            }
        }
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return null;
        }
        fast = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
